package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.entity.vo.request.EduCommentRequestVo;
import com.atguigu.eduservice.service.EduCommentService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "课程评论接口")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/comment")
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    //1.分页查询评论
    @ApiOperation("分页查询评论")
    @PostMapping("pageComment/{current}/{limit}")
    public R pageComment(@PathVariable long current, @PathVariable long limit,
                         @RequestBody(required = false) EduCommentRequestVo.EduCommentPageQueryRequest request) {
        //创建page对象
        Page<EduComment> commentPage = new Page<>(current, limit);

        //构建条件
        LambdaQueryWrapper<EduComment> wrapper = new LambdaQueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql

        String courseId = request.getCourseId();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(courseId)) {
            //构建条件
            wrapper.like(EduComment::getCourseId, courseId);
        }
        //排序
        wrapper.orderByDesc(EduComment::getGmtCreate);

        //调用方法实现条件查询分页
        eduCommentService.page(commentPage, wrapper);

        long total = commentPage.getTotal();//总记录数
        List<EduComment> records = commentPage.getRecords(); //数据list集合
        return R.ok().data("total", total).data("rows", records);
    }

    //2.添加评论
    @ApiOperation("添加评论")
    @PostMapping("addComment")
    public R addTeacher(@RequestBody EduCommentRequestVo.EduCommentAddCommentRequest request ) {
        boolean save = eduCommentService.addComment(request);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //3.删除评论


}
